﻿using Cms.Core.Data.Infrastructure;
using Cms.Core.Infrastructure.Extension;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using System;
using System.Collections.Generic;
using System.Text;

namespace Cms.Core.Data.UnitOfWork
{
    public static class UnitOfWorkServiceCollectionExtensions
    {
        public static IServiceCollection AddUnitOfWork<TContext>(this IServiceCollection services) where TContext : DbContext
        {
            services.AddScoped<IUnitOfWork, UnitOfWork<TContext>>();

            return services;
        }

        public static IServiceCollection AddRepository(this IServiceCollection services)
        {
            services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
            services.AddScoped(typeof(IRepository<,>), typeof(Repository<,>));
            return services;
        }

        public static IServiceCollection AddDbContext<TContext>(this IServiceCollection services, string connectionString, bool useRowNumberForPaging = true) where TContext : DbContext
        {
            connectionString.NotNullOrEmpty(nameof(connectionString));
            services.AddDbContext<TContext>(
              
               options => options.UseSqlServer(connectionString, b => b.UseRowNumberForPaging(useRowNumberForPaging))
            );
            return services;
        }

   

    }
}
